package leetcode.D300.T295;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @Author: 18362
 * @Create: 2022-04-08 22:28:48 星期五
 */

class MedianFinder {

    private Queue<Integer> small = new PriorityQueue<>((a, b) -> {
        return b - a;
    });
    private Queue<Integer> large = new PriorityQueue<>();

    public MedianFinder() {

    }

    public void addNum(int num) {
        if (small.size() == large.size()) {
            large.offer(num);
            small.offer(large.poll());
        } else {
            small.offer(num);
            large.offer(small.poll());
        }
    }

    public double findMedian() {
        if (small.size() == large.size())
            return (small.peek()+large.peek())/2.0;
        else
            return small.peek();
    }
}